{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import re\n",
    "import shutil\n",
    "from tqdm import tqdm\n",
    "from PIL import Image\n",
    "\n",
    "folder_name = 'Sprite'\n",
    "files = os.listdir(folder_name)\n",
    "skill_regex = r'^(Skill.*|^MonsterSkill.*)_HD.png'\n",
    "status_regex = r'^UI_Gcg_Buff_(.*).png'\n",
    "cardface_regex = r'^UI_Gcg_CardFace_((Assist_[^_]*_|Char_[^_]*_|Event_[^_]*_|Modify_[^_]*_|Summon).*).png$'\n",
    "small_card_regex = r'^UI_Gcg_History_(.*)_S.png$'\n",
    "avatar_regex = r'^UI_Gcg_Char_.*Icon_(.*).png'\n",
    "tag_regex = r'^UI_Gcg_Tag_(.*).png'\n",
    "mark_regex = r'^UI_TeyvatCard_((LifeBg|Preview|Revive|Select|Death).*).png'\n",
    "dice_regex = r'UI_Gcg_DiceS_(.*).png'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['skill_regex',\n",
       " 'status_regex',\n",
       " 'cardface_regex',\n",
       " 'small_card_regex',\n",
       " 'avatar_regex',\n",
       " 'tag_regex',\n",
       " 'mark_regex',\n",
       " 'dice_regex']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "keys = [x for x in globals().keys() if re.match(r'^.*_regex$', x)]\n",
    "keys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "avatar_regex 93\n"
     ]
    }
   ],
   "source": [
    "keys = ['avatar_regex']\n",
    "for key in keys:\n",
    "    fname = key.replace('_regex', '')\n",
    "    regex = globals()[key]\n",
    "    if not os.path.exists(fname):\n",
    "        os.mkdir(fname)\n",
    "    counter = 0\n",
    "    for file in files:\n",
    "        m = re.match(regex, file)\n",
    "        if m:\n",
    "            newname = m.group(1)\n",
    "            if key == 'cardface_regex':\n",
    "                # if is cardface, need more check\n",
    "\n",
    "                # if has golden, skip\n",
    "                if 'Golden' in file:\n",
    "                    continue\n",
    "                # if name contains #number, remove it from name\n",
    "                newname = re.sub(r'#\\d+', '', newname)\n",
    "                newname = re.sub(r'_Layer\\d+', '', newname)\n",
    "                newname += '.png'\n",
    "                if os.path.exists(os.path.join(fname, newname)):\n",
    "                    # exists, check both image size\n",
    "                    this_size = Image.open(os.path.join(folder_name, file)).size\n",
    "                    target_size = Image.open(os.path.join(fname, newname)).size\n",
    "                    if this_size[0] < target_size[0]:\n",
    "                        # this is smaller, skip\n",
    "                        continue\n",
    "            else:\n",
    "                newname += '.png'\n",
    "            # newname = file\n",
    "            # copy file to folder\n",
    "            shutil.copy(os.path.join(folder_name, file), os.path.join(fname, newname))\n",
    "            counter += 1\n",
    "    print(key, counter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Char_Avatar\n",
      "Modify_Talent\n",
      "Assist_NPC\n",
      "Summon_AbyssEle\n",
      "Event_Event\n",
      "Char_Monster\n",
      "Assist_Prop\n",
      "Event_Food\n",
      "Event_ResonanceB\n",
      "Modify_Constellation\n",
      "Modify_Artifact\n",
      "Modify_Weapon\n",
      "Event_ResonanceA\n",
      "Assist_Location\n",
      "Summon\n",
      "Char_Enemy\n"
     ]
    }
   ],
   "source": [
    "# check img size of cardface\n",
    "for file in os.listdir('cardface'):\n",
    "    size = Image.open(os.path.join('cardface', file)).size\n",
    "    if size[0] != 420:\n",
    "        print(file, size)\n",
    "# check file prefix\n",
    "prefix = set()\n",
    "for file in os.listdir('cardface'):\n",
    "    prefix.add('_'.join(file.split('_')[:-1]))\n",
    "for p in prefix:\n",
    "    print(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "# move Texture2D files to mark\n",
    "files = os.listdir('Texture2D')\n",
    "for file in files:\n",
    "    newname = file[:-4]\n",
    "    newname = newname.replace('_Bg', '')\n",
    "    newname = newname.split('_')[-1]\n",
    "    shutil.copy(os.path.join('Texture2D', file), os.path.join('mark', newname + '.png'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# check small_card all exist in cardface\n",
    "small_card = set(os.listdir('small_card'))\n",
    "cardface = set(os.listdir('cardface'))\n",
    "assert small_card.issubset(cardface)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "GITCG",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
